
package com.rinvay.asdownloader.core.access;

import com.rinvay.asdownloader.platform.AndroidFactory;

import android.content.Context;
import android.telephony.TelephonyManager;


/**
 * Mobile access network
 * 
 */
public class MobileNetworkAccess extends NetworkAccess {
	/**
	 * Telephony manager
	 */
	private TelephonyManager telephonyManager;
	

	/**
	 * Constructor
	 * 
     * @throws CoreException
	 */
	public MobileNetworkAccess(){
		super();

		// Get telephony info
		telephonyManager = (TelephonyManager)AndroidFactory.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);

//		if (logger.isActivated()) {
//    		logger.info("Mobile access has been created (interface " + getNetworkName() + ")");
//    	}
    }
			
	/**
     * Connect to the network access
     * 
     * @param ipAddress IP address
     */
    public void connect(String ipAddress) {
//    	if (logger.isActivated()) {
//    		logger.info("Connecting to " + ipAddress);
//    	}
		this.ipAddress = ipAddress;
    }
    
	/**
     * Disconnect from the network access
     */
    public void disconnect() {
//    	if (logger.isActivated()) {
//    		logger.info("Disconnecting from " + ipAddress);
//    	}
    	ipAddress = null;
    }
    
	/**
	 * Return the type of access
	 * 
	 * @return Type
	 */
	public String getType() {
		int type = telephonyManager.getNetworkType();
		switch(type) {
			case TelephonyManager.NETWORK_TYPE_GPRS:
			case TelephonyManager.NETWORK_TYPE_EDGE:
				return "3GPP-GERAN";
			case TelephonyManager.NETWORK_TYPE_UMTS:
			case TelephonyManager.NETWORK_TYPE_HSDPA:
			case TelephonyManager.NETWORK_TYPE_HSUPA:
			case TelephonyManager.NETWORK_TYPE_HSPA:
				return "3GPP-UTRAN-FDD";
			default:
				return null;
		}
	}

	/**
	 * Return the network name
	 * 
	 * @return Name
	 */
	public String getNetworkName() {
		int type = telephonyManager.getNetworkType();
		switch(type) {
			case TelephonyManager.NETWORK_TYPE_GPRS:
				return "GPRS";
			case TelephonyManager.NETWORK_TYPE_EDGE:
				return "EDGE";
			case TelephonyManager.NETWORK_TYPE_UMTS:
				return "UMTS";
			case TelephonyManager.NETWORK_TYPE_HSDPA:
				return "HSDPA";
			case TelephonyManager.NETWORK_TYPE_HSUPA:
				return "HSUPA";
			case TelephonyManager.NETWORK_TYPE_HSPA:
				return "HSPA";
			default:
				return "unknown";
		}
	}
}
